
package chatclient;

import java.io.IOException;
import model.Constantes;
import model.DataMssge;

/**
 *
 * @author carlos
 */
public class FCrearCuenta extends javax.swing.JFrame {

    ChatClient parent = null;

    /**
     * Creates new form FCrearCuenta
     */
    public FCrearCuenta(ChatClient cliente) {
        initComponents();

        // gets the parent Frame object in order to activate it on close
        this.parent = cliente;

        // inicializes error icons
        initializeErrorIcons();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblName = new javax.swing.JLabel();
        lblPassword = new javax.swing.JLabel();
        lblErrorMessage = new javax.swing.JLabel();
        cmdConnect = new javax.swing.JButton();
        lblUsername = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        cmdCancel = new javax.swing.JButton();
        txtName = new javax.swing.JTextField();
        lblTitle = new javax.swing.JLabel();
        txtLastname = new javax.swing.JTextField();
        lblLastname = new javax.swing.JLabel();
        lblMail = new javax.swing.JLabel();
        txtMail = new javax.swing.JTextField();
        lblRePassword = new javax.swing.JLabel();
        lblIconMail = new javax.swing.JLabel();
        lblIconUsername = new javax.swing.JLabel();
        lblIconPasswd = new javax.swing.JLabel();
        lblIconRePasswd = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        txtRePassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Chat Application - New Account");
        setAlwaysOnTop(true);
        setMinimumSize(new java.awt.Dimension(332, 293));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        lblName.setText("Name:");

        lblPassword.setText("Pass: ");

        lblErrorMessage.setForeground(new java.awt.Color(255, 51, 51));
        lblErrorMessage.setName("lblErrorMessage");

        cmdConnect.setText("send");
        cmdConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdConnectActionPerformed(evt);
            }
        });

        lblUsername.setText("Username:");

        cmdCancel.setText("cancel");
        cmdCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelActionPerformed(evt);
            }
        });

        lblTitle.setFont(new java.awt.Font("Lucida Calligraphy", 1, 14)); // NOI18N
        lblTitle.setForeground(new java.awt.Color(0, 102, 204));
        lblTitle.setText("Create a new account");

        lblLastname.setText("Last name:");

        lblMail.setText("E-Mail:");

        lblRePassword.setText("Re- Pass:");

        lblIconMail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tools/notification_error.png"))); // NOI18N
        lblIconMail.setToolTipText("email no valido");

        lblIconUsername.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tools/notification_error.png"))); // NOI18N
        lblIconUsername.setToolTipText("usuario no valido");

        lblIconPasswd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tools/notification_error.png"))); // NOI18N
        lblIconPasswd.setToolTipText("la contraseña debe ser mayor de 6 caracteres");

        lblIconRePasswd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tools/notification_error.png"))); // NOI18N
        lblIconRePasswd.setToolTipText("contraseña reecrita incorrectamente");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblErrorMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblTitle)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lblUsername)
                                    .addComponent(lblName)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(7, 7, 7)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lblPassword)
                                    .addComponent(lblMail)
                                    .addComponent(lblRePassword)))
                            .addComponent(lblLastname, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(cmdCancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmdConnect)
                                .addGap(55, 55, 55))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtPassword, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtMail, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtLastname, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtName, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtUsername, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtRePassword, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblIconUsername)
                                    .addComponent(lblIconMail)
                                    .addComponent(lblIconPasswd)
                                    .addComponent(lblIconRePasswd))))))
                .addGap(0, 26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(lblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblErrorMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblUsername)
                        .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblIconUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblName)
                            .addComponent(txtName))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblLastname)
                            .addComponent(txtLastname))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblMail)
                            .addComponent(txtMail)))
                    .addComponent(lblIconMail, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblPassword)
                        .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblIconPasswd, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblRePassword)
                        .addComponent(txtRePassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblIconRePasswd, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdCancel)
                    .addComponent(cmdConnect))
                .addGap(24, 24, 24))
        );

        getAccessibleContext().setAccessibleParent(this);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdConnectActionPerformed
        boolean blnHasErrors = false;
        DataMssge datos = new DataMssge();

        initializeErrorIcons();

        // Check inserted data
        if (!ValidationMethods.isUserValid(txtUsername.getText())) {
            blnHasErrors = true;
            lblIconUsername.setVisible(true);
        }
        // Check inserted data
        if (!ValidationMethods.isMailValid(txtMail.getText())) {
            blnHasErrors = true;
            lblIconMail.setVisible(true);
        }
        // Check inserted data
        if (!ValidationMethods.isPasswordValid(txtPassword.getText())) {
            blnHasErrors = true;
            lblIconPasswd.setVisible(true);
        }
        // Check inserted data
        if (!ValidationMethods.isPasswordsEqual(txtRePassword.getText(), txtPassword.getText())) {
            blnHasErrors = true;
            lblIconRePasswd.setVisible(true);
        }

        if (!blnHasErrors) {
            datos.setOption(Constantes.Eoption.crearcuenta);
            datos.getUser().setUsername(txtUsername.getText());
            datos.getUser().setName(txtName.getText());
            datos.getUser().setLastName(txtLastname.getText());
            datos.getUser().setMail(txtMail.getText());
            datos.getUser().setPassword(txtPassword.getText());

            try {
                this.parent.getComm().sendRequest(datos);
            } catch (IOException ex) {
                this.parent.ConnectionClosed();
                Common.ErrorMessage("Error al mandar peticion: " + ex.getMessage(), ex);
            }
        }
    }//GEN-LAST:event_cmdConnectActionPerformed

    private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelActionPerformed
        // closes the form
        this.closeWindow();
    }//GEN-LAST:event_cmdCancelActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        this.parent.showParent();
    }//GEN-LAST:event_formWindowClosed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdCancel;
    private javax.swing.JButton cmdConnect;
    private javax.swing.JLabel lblErrorMessage;
    private javax.swing.JLabel lblIconMail;
    private javax.swing.JLabel lblIconPasswd;
    private javax.swing.JLabel lblIconRePasswd;
    private javax.swing.JLabel lblIconUsername;
    private javax.swing.JLabel lblLastname;
    private javax.swing.JLabel lblMail;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblRePassword;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JTextField txtLastname;
    private javax.swing.JTextField txtMail;
    private javax.swing.JTextField txtName;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JPasswordField txtRePassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables

    private void initializeErrorIcons() {
        lblIconMail.setVisible(false);
        lblIconUsername.setVisible(false);
        lblIconPasswd.setVisible(false);
        lblIconRePasswd.setVisible(false);
    }

    public void setErrorLabel(String strMsg) {
        this.lblErrorMessage.setText(strMsg);
    }

    private void closeWindow() {
        this.setVisible(false);
        this.dispose();
    }
}
